package net.sunofbeach.blog.services.impl;

import lombok.extern.slf4j.Slf4j;
import net.sunofbeach.blog.dao.GuideIpDao;
import net.sunofbeach.blog.pojo.GuideIp;
import net.sunofbeach.blog.response.ResponseResult;
import net.sunofbeach.blog.services.IGuideIpService;
import net.sunofbeach.blog.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

@Slf4j
@Service
@Transactional
public class GuideIpServiceImpl  extends BaseService implements IGuideIpService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private GuideIpDao guideIpDao;

    @Override
    public ResponseResult postGuideIp(GuideIp guideIp, HttpServletRequest request) {
        guideIp.setId(idWorker.nextId() + "");
        guideIp.setCreateTime(new Date());
        String remoteAddr = request.getRemoteAddr();
        String remoteHost = request.getRemoteHost();
        String localAddr = request.getLocalAddr();

        String ipAddr = getIpAddr(request);

        log.info("我来打印ip==========>" + ipAddr);
        guideIp.setLoginIp(remoteAddr);
        guideIp.setRegIp(ipAddr);

        guideIpDao.save(guideIp);
        return ResponseResult.SUCCESS();
    }

    @Override
    public ResponseResult listGuideIps(int page, int size) {
        page = checkPage(page);
        size = checkSize(size);
        //没有就往下走
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        Page<GuideIp> all = guideIpDao.findAll(pageable);
        return ResponseResult.SUCCESS("获取评论列表成功").setData(all);
    }

    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress="";
        }
        // ipAddress = this.getRequest().getRemoteAddr();

        return ipAddress;
    }
}

